/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.koller.gravity.scene.map;

import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JTextField;

/**
 *
 * @author User
 */
public class MapSceneTester extends javax.swing.JFrame {

	private final MapScene mapgen = new MapScene();
	/**
	 * Creates new form MapSceneTester
	 */
	public MapSceneTester() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        startDetail = new javax.swing.JSlider();
        sdlabel = new javax.swing.JLabel();
        detlabel = new javax.swing.JLabel();
        detailSteps = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        water = new javax.swing.JTextField();
        beach = new javax.swing.JTextField();
        gras = new javax.swing.JTextField();
        snow = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        persistence = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        seed = new javax.swing.JTextField();
        mapview = new de.koller.gravity.scene.map.MapView();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Perlin-Noise ");
        setLocationByPlatform(true);
        setResizable(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Parameter"));

        jButton1.setText("neu generieren");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        startDetail.setMaximum(9);
        startDetail.setToolTipText("Setzt den Detailgrad (Frequenz) mit der der Algorithmus beginnen soll.");
        startDetail.setValue(3);
        startDetail.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                startDetailStateChanged(evt);
            }
        });

        sdlabel.setText("Startdetailgrad:");

        detlabel.setText("Detailstufen:");

        detailSteps.setMaximum(9);
        detailSteps.setToolTipText("Anzahl der Detailstufen. Achtung: Je höher, desto länger dauert die Berechnung.");
        detailSteps.setValue(6);
        detailSteps.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                detailStepsStateChanged(evt);
            }
        });
        detailSteps.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                detailStepsPropertyChange(evt);
            }
        });

        jLabel2.setText("Wasser-, Strand-, Gras-, Schnee- höhen:");

        water.setBackground(new java.awt.Color(0, 192, 255));
        water.setText("5");
        water.setToolTipText("Höhe der Wasserschicht");

        beach.setBackground(new java.awt.Color(255, 255, 1));
        beach.setText("1");
        beach.setToolTipText("Höhe des Strandes");

        gras.setBackground(new java.awt.Color(0, 255, 0));
        gras.setText("3");
        gras.setToolTipText("Höhe der Grasschicht");

        snow.setText("1");
        snow.setToolTipText("Höhe der Schneegipfel");

        jLabel5.setText("Persistenz :");

        persistence.setText("0.5");
        persistence.setToolTipText("Gibt an wie groß die Amplitude bei höheren Detailstufen abschwächt.");

        jLabel6.setText("Seed:");

        seed.setToolTipText("Text eingeben, bei dem bei gleichen Einstellungen immer die selbe Karte erzeugt wird. Leer lassen für zufälligen Seed.");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(startDetail, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(detlabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4))
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(detailSteps, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sdlabel)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(water, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(beach, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(gras, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(snow, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(seed)
                            .addComponent(persistence))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sdlabel)
                .addGap(3, 3, 3)
                .addComponent(startDetail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(detlabel)
                    .addComponent(jLabel4))
                .addGap(3, 3, 3)
                .addComponent(detailSteps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(water, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(beach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(gras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(snow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(persistence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(seed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(6, 6, 6))
        );

        javax.swing.GroupLayout mapviewLayout = new javax.swing.GroupLayout(mapview);
        mapview.setLayout(mapviewLayout);
        mapviewLayout.setHorizontalGroup(
            mapviewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );
        mapviewLayout.setVerticalGroup(
            mapviewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mapview, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(mapview, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		genImg();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void detailStepsPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_detailStepsPropertyChange
		
    }//GEN-LAST:event_detailStepsPropertyChange

    private void detailStepsStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_detailStepsStateChanged
		int s = detailSteps.getValue() + startDetail.getValue();
		
		if( s > 9 )
			startDetail.setValue( 9 - detailSteps.getValue() );
		
		sdlabel.setText( "Startdetailgrad: " + startDetail.getValue() );
		detlabel.setText( "Detailstufen: " + detailSteps.getValue() );
//		this.repaint();
    }//GEN-LAST:event_detailStepsStateChanged

    private void startDetailStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_startDetailStateChanged
        int s = detailSteps.getValue() + startDetail.getValue();
		
		if( s > 9 )
			detailSteps.setValue( 9 - startDetail.getValue() );
		
		sdlabel.setText( "Startdetailgrad: " + startDetail.getValue() );
		detlabel.setText( "Detailstufen: " + detailSteps.getValue() );
//		this.repaint();
    }//GEN-LAST:event_startDetailStateChanged

	public void genImg() {
		mapgen.steps = detailSteps.getValue();
		mapgen.startStep = startDetail.getValue();
		
		float w = validFloat( water );
		float b = validFloat( beach );
		float g = validFloat( gras );
		float s = validFloat( snow );
		
		float n = w + b + g + s;
		if( n == 0 ) {
			g = 1;
			n = 1;
		}
		
		w /= n;
		b /= n;
		g /= n;
		s /= n;
		
		mapgen.waterheight = w;
		mapgen.beachheight = w + b;
		mapgen.snowheight = 1 - s;
		
		
		try {
			float v = Float.parseFloat( persistence.getText() );
			mapgen.persistence = v;
		} catch( Exception ex ) {
			persistence.setText( "0.5" );
		}
		
		if( !seed.getText().isEmpty() )
			mapgen.rnd = new Random( seed.getText().hashCode() );
			
		BufferedImage map = mapgen.renderScene();
		mapview.paintMap( map );
	}
	
	private float validFloat( JTextField text ) {
		try {
			float v = Float.parseFloat( text.getText() );
			if( v >= 0 )
				return v;
			
			text.setText( "0" );
			return 0;
		} catch( Exception ex ) {
			text.setText( "0" );
			return 0;
		}
	}
	
	/**
	 * @param args the command line arguments
	 */
	public static void main( String args[] ) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels() ) {
				if( "Nimbus".equals( info.getName() ) ) {
					javax.swing.UIManager.setLookAndFeel( info.getClassName() );
					break;
				}
			}
		} catch ( ClassNotFoundException ex ) {
			java.util.logging.Logger.getLogger( MapSceneTester.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		} catch ( InstantiationException ex ) {
			java.util.logging.Logger.getLogger( MapSceneTester.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		} catch ( IllegalAccessException ex ) {
			java.util.logging.Logger.getLogger( MapSceneTester.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		} catch ( javax.swing.UnsupportedLookAndFeelException ex ) {
			java.util.logging.Logger.getLogger( MapSceneTester.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater( new Runnable() {
			public void run() {
				new MapSceneTester().setVisible( true );
			}
		} );
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField beach;
    private javax.swing.JSlider detailSteps;
    private javax.swing.JLabel detlabel;
    private javax.swing.JTextField gras;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private de.koller.gravity.scene.map.MapView mapview;
    private javax.swing.JTextField persistence;
    private javax.swing.JLabel sdlabel;
    private javax.swing.JTextField seed;
    private javax.swing.JTextField snow;
    private javax.swing.JSlider startDetail;
    private javax.swing.JTextField water;
    // End of variables declaration//GEN-END:variables
}
